package com.yidu.ruifeng_logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.ruifeng_logistics.entity.LogisticsArea;
import com.yidu.ruifeng_logistics.entity.LogisticsOrderVo;
import com.yidu.ruifeng_logistics.entity.LogisticsWaybill;
import com.yidu.ruifeng_logistics.service.LogisticsAreaService;
import com.yidu.ruifeng_logistics.service.LogisticsWaybillService;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import com.yidu.ruifeng_logistics.utils.IdWorker;
import com.yidu.ruifeng_logistics.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 运单表(LogisticsWaybill)表控制层
 *
 * @author makejava
 * @since 2021-03-12 15:34:23
 */
@Api(tags = "运单接口")
@RestController
@RequestMapping("logisticsWaybill")
public class LogisticsWaybillController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsWaybillService logisticsWaybillService;

    /*
地区
*/
    @Resource
    private LogisticsAreaService logisticsAreaService;

    /**
     * 查询运单
     * @param logisticsWaybill
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询运单", notes = "返回运单集合")
    @GetMapping("select")
    public Map<String, Object> select(LogisticsWaybill logisticsWaybill, Integer page,  Integer limit){

        IdWorker idWorker = new IdWorker();
        Long waybillId = idWorker.nextId();

        //将参数传入分页助手 PageHelper.startPage(page,limit);
        PageHelper.startPage(page,limit);
        List<LogisticsWaybill> list=logisticsWaybillService.queryAll(logisticsWaybill);
        for (LogisticsWaybill vo :list){
            //根据出发地ID查询地名
            List<LogisticsArea> area = logisticsAreaService.selectYi(Integer.valueOf(vo.getWaybillDeparture()));
            for(LogisticsArea area1:area){
                vo.setWaybillDeparture(area1.getAreaName());
            }
            //根据目的地ID查询地名
            List<LogisticsArea> areas = logisticsAreaService.selectYi(Integer.valueOf(vo.getWaybillBourn()));
            for(LogisticsArea area1:areas){
                vo.setWaybillBourn(area1.getAreaName());
            }
        }
        //查询到集合后，将集合数据封装到分页助手的PageInfo对象中
        PageInfo<LogisticsWaybill> pageInfo=new PageInfo<>(list);
        //返回Tools的Layuimap集合
        return Tools.getLayUiMap(list,(int)pageInfo.getTotal());
    }

    /**
     * 修改运单
     * @param logisticsWaybill
     * @return
     */
    @ApiOperation(value = "修改运单", notes = "返回状态")
    @PostMapping("updata")
    public BaseMessage updata(LogisticsWaybill logisticsWaybill){

        BaseMessage baseMessage = new BaseMessage();
        //logisticsWaybill.getWaybillId()不为空则修改运单
        if (logisticsWaybill.getWaybillId() != null) {
            logisticsWaybillService.update(logisticsWaybill);
            baseMessage.setState(1);
            baseMessage.setMsg("修改成功");
        }else {
            baseMessage.setState(0);
            baseMessage.setMsg("修改失败");
        }
        return baseMessage;
    }
}
